package com.hlf.modules.sys.service.impl;

import com.hlf.common.utils.ReturnResult;
import com.hlf.modules.sys.entity.SysUserTokenEntity;
import com.hlf.modules.sys.mapper.SysUserTokenMapper;
import com.hlf.modules.sys.oauth2.TokenGenerator;
import com.hlf.modules.sys.service.SysUserTokenService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;


@Service("sysUserTokenService")
public class SysUserTokenServiceImpl implements SysUserTokenService {
    @Autowired
    private SysUserTokenMapper sysUserTokenMapper;
    //两个月后过期
    private static final Long EXPIRE = 3600 * 24 * 30 * 2L;

    @Override
    public SysUserTokenEntity queryByUserId(Long userId) {
        return sysUserTokenMapper.queryByUserId(userId);
    }

    @Override
    public SysUserTokenEntity queryByToken(String token) {
        return sysUserTokenMapper.queryByToken(token);
    }

    @Override
    public void save(SysUserTokenEntity token) {
        sysUserTokenMapper.save(token);
    }

    @Override
    public void update(SysUserTokenEntity token) {
        sysUserTokenMapper.update(token);
    }

    @Override
    public Map createToken(long userId) {
        //生成一个token
        String token = TokenGenerator.generateValue();
        //测试token
        if (userId == 10) {
            token = "123456";
        }

        //当前时间
        Date now = new Date();
        //过期时间
        Date expireTime = new Date(now.getTime() + EXPIRE * 1000);

        //判断是否生成过token
        SysUserTokenEntity tokenEntity = queryByUserId(userId);
        if (tokenEntity == null) {
            tokenEntity = new SysUserTokenEntity();
            tokenEntity.setUserId(userId);
            tokenEntity.setToken(token);
            tokenEntity.setUpdateTime(now);
            tokenEntity.setExpireTime(expireTime);

            //保存token
            save(tokenEntity);
        } else {
            tokenEntity.setToken(token);
            tokenEntity.setUpdateTime(now);
            tokenEntity.setExpireTime(expireTime);

            //更新token
            update(tokenEntity);
        }
        Map map = new HashMap();
        map.put("token", token);
        map.put("expire", EXPIRE);

        return map;
    }

    @Override
    public ReturnResult sysCreateToken(long userId) {

        Map token = createToken(userId);
        return ReturnResult.ok().put("token", token.get("token")).put("expire", EXPIRE);
    }

    @Override
    public ReturnResult deleteToken(long userId) {
        sysUserTokenMapper.deleteForId(userId);
        return ReturnResult.ok();
    }

    @Override
    public void logout(long userId) {
        //生成一个token
        String token = TokenGenerator.generateValue();

        //修改token
        SysUserTokenEntity tokenEntity = new SysUserTokenEntity();
        tokenEntity.setUserId(userId);
        tokenEntity.setToken(token);
        update(tokenEntity);
    }

}
